﻿
using System ;

using MskEdit.Msk.Representation ;


namespace MskEdit.Msk.Model
{

    public class MskFunctionCode
    {
#region Private data
        
        private static  MskFunctionCode nullFunctionCode_ = new MskFunctionCode() ;
        private         int             functionCode_ ;
        
#endregion

#region Private foundation

        public MskFunctionCode()
        {
            functionCode_ = 1 ;
        }

        public MskFunctionCode( int functionCode )
        {
            functionCode_ = functionCode ;
        }

#endregion

#region Public foundation


        public static MskFunctionCode TryFrom( string line, MskFunctionCode fallbackValue ) {
        
            int? functionCode = IntField.PageCode.TryFrom( line, null ) ;
            if ( ! functionCode.HasValue )
                return fallbackValue ;
                
            return new MskFunctionCode( functionCode.Value ) ;
        }
        
        
        public override String ToString() {
            
            string toString = "" ;
            toString += "( " + functionCode_.ToString() + " )" ;
            
            if ( ( functionCode_ & 1 ) != 0 )
                toString += ", Reserved1" ;
                
            if ( ( functionCode_ & 2 ) != 0 )
                toString += ", Constant" ;
            
            if ( ( functionCode_ & 4 ) != 0 )
                toString += ", Reserved4" ;
            
            if ( ( functionCode_ & 8 ) != 0 )
                toString += ", Reserved8" ;
            
            if ( ( functionCode_ & 16 ) != 0 )
                toString += ", DFX Write" ;
            
            if ( ( functionCode_ & 32 ) != 0 )
                toString += ", DFX Read" ;
            
            if ( ( functionCode_ & 64 ) != 0 )
                toString += ", DFX Text" ;
            
            if ( ( functionCode_ & 128 ) != 0 )
                toString += ", DFX Attribute" ;
            
            if ( ( functionCode_ & 256 ) != 0 )
                toString += ", DFX No create" ;
            
            if ( ( functionCode_ & 512 ) != 0 )
                toString += ", Global" ;
            
            if ( ( functionCode_ & 512 ) != 0 )
                toString += ", Force display" ;
                
            toString = toString.Replace( " ),", " )" ) ;
            
            return toString ;
        }
        
#endregion
        
#region Public properties

        public static MskFunctionCode NullFunctionCode {
            get { return nullFunctionCode_ ; }
        }
        
#endregion

    }
}
